package com.interagriculture.controller;

import com.interagriculture.annotation.AdminVerify;
import com.interagriculture.common.Constants;
import com.interagriculture.common.CustomException;
import com.interagriculture.common.FilePathConstants;
import com.interagriculture.common.Result;
import com.interagriculture.enums.ExceptionCodeEnum;
import com.interagriculture.utils.CacheContainerUtil;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;

/**
 * 上传图片  //弃用 在临时文件里找不到 multipartFile
 * @author 11 2024/11/11
 */
@RestController
@RequestMapping("/picture")
public class PicturePlaceController {
    /**
     * 处理上传图片到缓存中
     * @param type
     * @param userId
     * @param picture
     * @return
     */
    @PostMapping("/uploading")
    @AdminVerify
    public Result uploading(MultipartFile picture,Integer type,Integer userId) throws IOException {
//        @RequestBody Integer type,
//        @RequestBody Integer userId,
        long size = picture.getSize();
        if(size> Constants.fiveM){
            throw new CustomException(ExceptionCodeEnum.PICTURE_TOO_LARGE);
        }
        StringBuffer stringBuffer = new StringBuffer();
        switch (type){
            case 1://实验室/地图
                stringBuffer.append(FilePathConstants.实验室图片地址);
                break;
            case 2://学生
                stringBuffer.append(FilePathConstants.学生图片地址).append(userId);
                break;
            case 3://老师
                stringBuffer.append(FilePathConstants.老师头像).append(userId);
                break;
            case 4://竞赛
                stringBuffer.append(FilePathConstants.比赛图片地址).append(userId);
                break;
        }
//        CacheContainerUtil.putFile(stringBuffer.toString(),picture);
        return Result.ok();
    }
}
